#!/bin/bash

Newimglist=`echo "$HOME/data/history/gft_observed.list"`
#Newimglistbak=`echo "$HOME/data//history/Yyyyy/gft_observed_yymmdd.list"`

stringYearForMonitorT=`date -u +%Y`
stringtimeForMonitorT=`date -u +%Y-%m-%d`
Dir_rawdata=`echo "$HOME/data/Y$stringYearForMonitorT/$stringtimeForMonitorT/mini-GWAC"`

stringtimeForMonitorT=`date -u +%Y%m%d`
dir_reduction=$HOME/workspace/reduction/$stringtimeForMonitorT
cd $HOME/workspace/reduction

if test ! -s $stringtimeForMonitorT                                                                                                                                      
then
    echo "making $stringtimeForMonitorT"
    mkdir $stringtimeForMonitorT
fi
cd $dir_reduction

echo "Reduce the fits in the history list [ Y/N ]:"
read AnswerWords
if [ "$AnswerWords"x == "Y"x ] 
then
    echo "OK, these fit images will be reduced one by one"
    sleep 1
else
    cat $Newimglist | column -t | awk '{print($1"/"$2)}' >oldlist
     
fi

xotSendMsg2Wechat03.sh

#echo "bias dark flat focus light" | xargs -n 1 >listdatafile 

xSendmsg2Wechat12 ( )
{
datetimestring=`date -u +%H%M%S`
SendMessage=`echo "W60ccd_is_processing_"$fitsname"_at_"$datetimestring`
#~/anaconda3/bin/python ~/xsoft/newxgwacmatchsoft/xSendmsg2WChart12.py $SendMessage

}



xobjectreduction (  )
{
    echo $fitsname
   # x60cmF_Plus2mass.sh $fitsname
   rm -rf /tmp/* 
  xSendmsg2Wechat12
  x60cmF_Plus2mass_20181022.sh $fitsname
#     x60cmF_usnob.sh $fitsname
}
xbiascheckandcombine (  )
{
    :
}
xdarkcheckandcombine (  )
{
    :
}
xflatcheckandcombine (  )
{
    :
}

xcopyobslist (  )
{
   # echo "xcopyobslist"
    if test ! -r oldlist
    then
        touch oldlist
    fi
    if test -s $Newimglist 
    then
        cp $Newimglist $dir_reduction
        # gft_observed.list is copied
        #cat gft_observed.list | awk '{print($1"/"$2)}' >newlist
        cat gft_observed.list | column -t | awk '{print($1"/"$2)}' >newlist
        diff oldlist newlist | grep ">" | tr -d '>' | column -t  >listmatch
        linedata=`cat listmatch | wc -l`
        if [ "$linedata" -ne 0  ]
        then
            echo "new image exits"
            cat listmatch | while read linedataroad
            do
                N_dataline=`echo $linedataroad | wc | awk '{print($2)}'`
                echo $linedataroad
                echo $N_dataline
                if [ $N_dataline -lt 1   ]
                then
                    echo "The information of this fits is not correct"
                else
                   # pathroad=`echo $linedataroad | awk '{print($1)}'`
                   # fitsname=`echo $linedataroad | awk '{print($2)}'`
                   # cp $pathroad"/"$fitsname $dir_reduction
			cp $linedataroad $dir_reduction
			fitsname=`echo $linedataroad | sed 's/\// /g' | tr ' ' '\n' | grep ".fit" | awk '{print($1)}'`
			echo $linedataroad >>oldlist
                    #echo "$pathroad $fitsname" | column -t   >>oldlist
                    xobjectreduction
                fi
            done
        else
            :
            sleep 1 
            continue
        fi 
    else
        echo "No any data"
        sleep 5
        continue
    fi

}

xcheckdatafile (  )
{

    xcopyobslist


    #for datafilename in `cat listotnamefile`
    #do
    #   Ab_datafilename=`echo $Dir_rawdata"/"$datafilename`
    #   echo $Ab_datafilename
    #   cd $Ab_datafilename
    #   xchecknewimage
    #done

}

while :
do
    cd $dir_reduction
    xCheckPoint.sh $Dir_reduction
    xcopyobslist
    #xcheckdatafile
done
